package com.post.subscribe.android;

import java.lang.reflect.InvocationTargetException;

import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.os.Looper;

import com.post.subscribe.Poster;
import com.post.subscribe.Subscirber;

public class AndroidPoster extends Poster{
	protected static Poster instance = new AndroidPoster();
	Handler handler;
	GLSurfaceView gl ;
	private AndroidPoster() {
		 handler = new Handler(Looper.getMainLooper());
	}

	public static Poster getDeafult() {
		return instance;
	}
	@Override
	protected void postInGL(final Object obj, final Subscirber item)
			throws IllegalAccessException, InvocationTargetException {
		super.postInGL(obj, item);
		if (gl!=null) {
			gl.queueEvent(new Runnable() {
				@Override
				public void run() {		
					try {
						item.method.invoke(item.object, obj);
					} catch (IllegalAccessException | IllegalArgumentException
							| InvocationTargetException e) {
						e.printStackTrace();
					}
					
				}
			});
		}
	}
	@Override
	protected void postInMain(final Object obj, final Subscirber item)
			throws IllegalAccessException, InvocationTargetException {
		super.postInMain(obj, item);
		handler.post(new Runnable() {
			@Override
			public void run() {
				try {
					item.method.invoke(item.object, obj);
				} catch (IllegalAccessException | IllegalArgumentException
						| InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		});
	}
}
